package com.atguigu.gulimall.minio.file.service.impl;

import com.atguigu.gulimall.minio.file.service.FileStorageService;
import com.atguigu.gulimall.minio.file.service.MinIoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
@Slf4j
public class MinIoServiceImpl implements MinIoService {
     @Autowired
    private FileStorageService fileStorageService;
      /**
     * 上传文件
     */
    @Override
    public String uploadPicture(MultipartFile multipartFile) {
          //1.检查参数


        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }
        //4.返回结果
        return fileId;
    }
}
